
package com.noah.project.dao

import com.noah.project.model.Friend
import com.noah.project.model.ReplyFriend
import org.apache.ibatis.annotations.*
import org.springframework.stereotype.Repository

@Repository
interface FriendDao {
    @Insert("INSERT INTO friend(fid, uid, relation, fTime) VALUES(#{fid}, #{uid}, #{relation}, #{fTime})")
    fun insert(friend: Friend): Long

    @Delete("DELETE FROM friend WHERE fid = #{fid} AND uid = #{uid}")
    fun delete(friend: Friend): Long

    @Update("UPDATE friend SET relation = #{relation} WHERE fid = #{fid} AND uid = #{uid}")
    fun update(friend: Friend): Long

    @Select("SELECT * FROM friend WHERE fid = #{fid} AND uid = #{uid}")
    fun findById(friend: Friend): Friend?

    @Select("SELECT * FROM friend LEFT JOIN user ON friend.uid = user.uid WHERE friend.fid = #{fid} AND friend.relation = #{relation} order by user.name asc LIMIT 10")
    @Results(
            Result(column = "uid", property = "user", one = One(select = "com.noah.project.dao.UserDao.findById")))
    fun findFriendList(fid: Long?, relation: Int): List<ReplyFriend>
}
